package fileloaders.mallet;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;

import puppy.graph.DeliciousNode;

import util.io.TextFileLoader;
import util.string.URLCleaner;

public class MalletTopicsProbs {

	/**
	 * @param args
	 */
	
	/**
	 * File format is word topic prob
	 * 
	 * 
	 * @param path
	 * @return
	 */
	public static void loadTopicGivenWord(String path, Hashtable<String,?> voc, Hashtable<String,Hashtable<Short,Float>> h){
		TextFileLoader loader=  new TextFileLoader(path);
	
		 while(loader.hasNext()){
			 
			 String t[] = loader.next();
			 String query = new String(t[0]);
			 String tt= new String(t[1]);
			 String p= new String(t[2]);
			 Short topic = Short.valueOf(tt);
			 Float prop = Float.valueOf(p);
			 
			 if(voc!=null&& !voc.containsKey(query)) continue;
		
			
			 tt=null;
			 p=null;
			 t=null;
			 if(h.containsKey(query)){
				 Hashtable<Short, Float> temp = h.get(query);
				 
				temp.put(topic,prop);
				 	
			 }else{
				 Hashtable<Short, Float> temp = new  Hashtable<Short, Float>();
				 temp.put(topic,prop);
				 h.put(query,temp);
				 
			 }
			 
			 query=null;
			 topic=null;
			 prop=null;
		 }
		
		
	}
	
	
	public static void loadTopicGivenWord(String path, Hashtable<String,Hashtable<Short,Float>> h){
		TextFileLoader loader=  new TextFileLoader(path);
	
		 while(loader.hasNext()){
			 
			 String t[] = loader.next();
			 String query = new String(t[0]);
			 String tt= new String(t[1]);
			 String p= new String(t[2]);
			 Short topic = Short.valueOf(tt);
			 Float prop = Float.valueOf(p);
			 
			
			
			 tt=null;
			 p=null;
			 t=null;
			 if(h.containsKey(query)){
				 Hashtable<Short, Float> temp = h.get(query);
				 
				temp.put(topic,prop);
				 	
			 }else{
				 Hashtable<Short, Float> temp = new  Hashtable<Short, Float>();
				 temp.put(topic,prop);
				 h.put(query,temp);
				 
			 }
			 
			 query=null;
			 topic=null;
			 prop=null;
		 }
		
		
	}
	
	
	
	
	
	
	/**
	 * borreme 
	 * 
	 * @param path
	 * @param v
	 * @param simplified
	 */
 public static void loadTopicGivenDoc(String path, Hashtable<String, Hashtable<Short,Float>> q_topics,Hashtable<String,?>v, Hashtable<String,String>simplified){
		TextFileLoader loader=  new TextFileLoader(path);
		 while(loader.hasNext()){
			 
			 String t[] = loader.next();
			 
			 if(t[0].contains("#doc")) continue;
			//System.out.println(t[0]);
			 String doc = new String(t[1]);
			
			 
			 if(simplified!=null && !simplified.containsKey(doc)) continue;
			 
			 String doc_original =doc;
			 
			 if(simplified !=null){
				 doc_original = simplified.get(doc);
				 
			 }
			 
		//	 System.out.println(doc + "\t"+ doc_original);
		//System.out.println(doc +"\t"+ );
			// String simplified = "";
			 
			 if( v==null || ( v!=null && !v.containsKey(doc_original))){
				 System.out.println(t[1]+ "\t"+doc);
				 t=null;

				 doc=null;
				 continue;
			 } 
			 
		//	 System.out.println(doc+ "\t"+ doc_original);
			
			 int j=2;
			 Short topic=-1;
			 Float prop;
			 
			
			 while(j< t.length){
					
				 if(j%2==0){ //category
					 topic = Short.valueOf(new String(t[j]));
					 
				 }else{ //prop
					 prop = Float.valueOf( new String(t[j]));
					 
					 if(!q_topics.containsKey(doc_original)){
						 Hashtable<Short, Float> temp = new Hashtable<Short,Float>();
						 temp.put(topic, prop);
						 q_topics.put(doc_original, temp);
					 }else{
						 
						q_topics.get(doc_original).put(topic, prop);
						 
					 }
					 
				 }
				 
				 j++;
			 }
			 
		 }
		// loader.close();
		
		
	}
	
 
 
 public static void loadTopicGivenDoc(String path, Hashtable<String, Hashtable<Short,Float>> q_topics){
		TextFileLoader loader=  new TextFileLoader(path);
		while(loader.hasNext()){
			 
			 String t[] = loader.next();
			 
			 if(t[0].contains("#doc")) continue;
			//System.out.println(t[0]);
			 String doc = new String(t[1]);
			 String doc_original =doc;
		
			 int j=2;
			 Short topic=-1;
			 Float prop;

			 while(j< t.length){
					
				 if(j%2==0){ //category
					 topic = Short.valueOf(new String(t[j]));
					 
				 }else{ //prop
					 prop = Float.valueOf( new String(t[j]));
					 
					 if(!q_topics.containsKey(doc_original)){
						 Hashtable<Short, Float> temp = new Hashtable<Short,Float>();
						 temp.put(topic, prop);
						 q_topics.put(doc_original, temp);
					 }else{
						 
						q_topics.get(doc_original).put(topic, prop);
						 
					 }
					 
				 }
				 
				 j++;
			 }
			 
		 }
		// loader.close();
		
		
	}
 
	
	
	public static void loadTopicGivenQueryShort(String path, Hashtable<String, Hashtable<Short,Float>> q_topics, HashSet<String> queries){
		TextFileLoader loader=  new TextFileLoader(path);
		 while(loader.hasNext()){
			 
			 String t[] = loader.next();
			 
			 if(t[0].contains("#doc")) continue;
			//System.out.println(t[0]);
			 String doc = new String(t[1]);
			
			 if(queries!=null && !queries.contains(doc)){
				
				 t=null;

				 doc=null;
				 continue;
			 } 
			 
		//	 System.out.println(doc+ "\t"+ doc_original);
			
			 int j=2;
			 Short topic=-1;
			 Float prop;
			 
	
			 while(j< t.length){
				
				 if(j%2==0){ //category
					 topic = Short.valueOf(new String(t[j]));
					 
				 }else{ //prop
					 prop = Float.valueOf( new String(t[j]));
					 
					 if(!q_topics.containsKey(doc)){
						 Hashtable<Short, Float> temp = new Hashtable<Short,Float>();
						 temp.put(topic, prop);
						 q_topics.put(doc, temp);
					 }else{
						 
						q_topics.get(doc).put(topic, prop);
						 
					 }
					 
				 }
				 
				 j++;
			 }
			 
		 }
	
		
		
	}
	
	public static void loadTopicGivenQuery(String path, Hashtable<String, Hashtable<String,Float>> q_topics, HashSet<String> queries){
		TextFileLoader loader=  new TextFileLoader(path);
		 while(loader.hasNext()){
			 
			 String t[] = loader.next();
			 
			 if(t[0].contains("#doc")) continue;
			//System.out.println(t[0]);
			 String doc = new String(t[1]);
			
			 if(queries!=null && !queries.contains(doc)){
				
				 t=null;

				 doc=null;
				 continue;
			 } 
			 
		//	 System.out.println(doc+ "\t"+ doc_original);
			
			 int j=2;
			String topic=null;
			 Float prop;
			 
	
			 while(j< t.length){
				
				 if(j%2==0){ //category
					 topic = new  String("t-"+t[j]);
					 
				 }else{ //prop
					 prop = Float.valueOf( new String(t[j]));
					 
					 if(!q_topics.containsKey(doc)){
						 Hashtable<String, Float> temp = new Hashtable<String,Float>();
						 temp.put(topic, prop);
						 q_topics.put(doc, temp);
					 }else{
						 
						q_topics.get(doc).put(topic, prop);
						 
					 }
					 
				 }
				 
				 j++;
			 }
			 
		 }
	}

	public static void loadTopicProbs(String path, Hashtable<String,Float>z_probs){
		
		TextFileLoader loader = new TextFileLoader(path);
		while(loader.hasNext()){
			String line[]  = loader.next();
			z_probs.put("t-"+line[0],Float.valueOf(line[1]));
			line =null;
		}
		
	}
	
	static String topic_model_urls="/Users/sergioduarte/projects/data/topic_model/k_t_ktm/100_output_doc_topics.txt";
	static String topic_model_tags="/Users/sergioduarte/projects/data/100_word_topic_weights_normalized.txt";
	
	static String topic_model_queries="/Users/sergioduarte/projects/data/topic_model/k_t_ktm/query/100__output_doc_topics_enriched.txt";
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		
		//Hashtable<String, Hashtable<Short, Float>> temp = loadTopicGivenWord(topic_model_tags);
	//	Hashtable<String, Hashtable<Short, Float>> temp2 = loadTopicGivenDoc(topic_model_urls);
	//	Hashtable<String, Hashtable<Short, Float>> temp3 = loadTopicGivenDoc(topic_model_queries);
		
	}

}
